const _vscode = acquireVsCodeApi()

let callBacks = {};
let vscode: VsCode = new Proxy({}, {
    get: (target, key) => {
        return function (...arg) {
            const cbid = Date.now() + '' + Math.round(Math.random() * 100000);
            let ps = new Promise((res, rj) => {
                _vscode.postMessage({
                    command: key,
                    data: arg,
                    cbid: cbid
                })
                callBacks[cbid] = res;
            })
            return ps;
        }
    }
}) as any;

interface VsCode {
    [key: string]: (...arg) => Promise<any>;
    /**显示提示 */
    showTips(str: string): Promise<any>;
    getFilePath(): Promise<string>;
    getWorkDir(): Promise<string>;
    go(path: string): Promise<void>;
}

window.onmessage = (event: MessageEvent) => {
    if (callBacks[event.data.cbid]) {
        (callBacks[event.data.cbid] as Function).call(window, event.data.data)
        delete callBacks[event.data.cbid];
    }
}
declare var acquireVsCodeApi: () => any;
export default vscode;